package com.magicborrow.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.beans.ConllectionListBean;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.huanxin.ImageUtils;
import com.magicborrow.utils.ImageLoadUtil;
import com.magicborrow.utils.UIHelper;

import java.util.ArrayList;

public class CollectionAdapter extends RefreshAdapter{

    private int width = 0;

    public CollectionAdapter(ArrayList<ConllectionListBean.DataEntity.ContentEntity> data) {
        super(data);
    }

    public void setWidth(int width){
        this.width = (width-10);
    }

    public ArrayList<ConllectionListBean.DataEntity.ContentEntity> getData(){
        return data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateItem(ViewGroup parent, int viewType) {
        return new PublishedRentViewHolder(LayoutInflater.from(context).inflate(R.layout.collection_item, null));
    }

    @Override
    public void onBindItem(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  PublishedRentViewHolder){
            PublishedRentViewHolder hold = (PublishedRentViewHolder)holder;
            //hold.layout.setLayoutParams(new LinearLayout.LayoutParams(width,width));
            final ConllectionListBean.DataEntity.ContentEntity bean = (ConllectionListBean.DataEntity.ContentEntity)data.get(position);
            String url = Constants.ADDRESS+"ssss.jpg";
            if(bean.getWare().getImages() != null && bean.getWare().getImages().size() > 0){
                url = Constants.ADDRESS+bean.getWare().getImages().get(0).getPreview();
            }
            ImageLoadUtil.loadImage(hold.pic, url,R.mipmap.default_image);
            Log.e("xxxx",width+"");
            hold.pic.setLayoutParams(new LinearLayout.LayoutParams(width,width));
            hold.title.setText(bean.getWare().getName());
            hold.price.setText(bean.getWare().getPrice()+"/"+ PriceModeEnum.getModeStr(bean.getWare().getPriceMode()));
            hold.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("data", bean.getWare());
                    intent.setClass(context, BorrowStuffDetailActivity.class);
                    context.startActivity(intent);
                }
            });
        }
    }

    public class PublishedRentViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout layout;
        private ImageView pic;
        private TextView title;
        private TextView price;
        private ImageView editor;

        public PublishedRentViewHolder(View view) {
            super(view);
            pic = (ImageView) view.findViewById(R.id.pic);
            title = (TextView) view.findViewById(R.id.title);
            price = (TextView) view.findViewById(R.id.price);
            editor = (ImageView) view.findViewById(R.id.editor);
            layout = (LinearLayout)view.findViewById(R.id.layout);
        }
    }

}
